﻿using System.Web.Mvc;
using WeixinMVC.Models;

namespace WeixinMVC.Controllers
{
    public class WeixinController : Controller
    {
        // GET: /Test/
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Enter()
        {
            var appid = Request.QueryString["appid"];
            if (string.IsNullOrWhiteSpace(appid))
            {
                ViewBag.result = "不允许空的appid";
                return View("Result");
            }

            return RedirectToAction("App/" + appid.Trim());
        }

        public ActionResult App(string id)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                ViewBag.errorMsg = "Id不存在";
                return View("WxError");
            }

            if (id.StartsWith("_"))
            {
                switch (id)
                {
                    case "_setting":
                        return PartialView("_WxSetting");
                    case "__setting":
                        var mode = new WeixinSDK.WxSetting() { AppId = "111", AppSecret = "222", UserToken = "3333" };
                        return Json(mode, JsonRequestBehavior.AllowGet);
                }
            }
            else
            {
                var model = new DataStore().GetWxSetting(id);
                return View("WxTest", model ?? new WeixinSDK.WxSetting() { AppId = id });
            }

            ViewBag.errorMsg = "未处理";
            return View("WxError");
        }
    }
}